//
//  ZEditVC.m
//  WeClubs
//
//  Created by Zed Link on 10/5/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZEditVC.h"
#import "ZNewTextViewCell.h"

NSString * EditTextViewCellReuseID = @"ZEditTextViewCellReuseID";

@interface ZEditVC () {
    NSString *_placeHolder;
    NSString *_inputText;
}

@end

@implementation ZEditVC

#pragma mark - About Navigation

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"确认" andSelector:@selector(submitAction)];
}

- (void)submitAction
{
//    if ([self.delegate respondsToSelector:@selector(editControllerDidSubmitText:)]) {
//        ZNewTextViewCell *textCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//        [self.delegate editControllerDidSubmitText:textCell.inputText];
//    }
    
    if (self.didSubmitText != nil) {
        ZNewTextViewCell *textCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        self.didSubmitText(textCell.inputText);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - View Life Cycle

- (void)zLoadSubview
{
    [super zLoadSubview];
    
    self.tableView.estimatedRowHeight = 150;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.bounces = NO;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZNewTextViewCell" bundle:nil] forCellReuseIdentifier:EditTextViewCellReuseID];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZNewTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:EditTextViewCellReuseID];
    [cell setPlaceHolderText:_placeHolder];
    [cell setInputText:_inputText];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1f;
}

#pragma mark - Public Method

- (void)setTitle:(NSString *)title andPlaceHolder:(NSString *)placeHolder
{
    self.navigationItem.title = title;
    _placeHolder = placeHolder;
    [self.tableView reloadData];
}

- (void)setInputText:(NSString *)inputText
{
    _inputText = inputText;
    [self.tableView reloadData];
}

@end
